package com.intelligentmeterreadingsys.waterlist.copywater.activity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;
import com.intelligentmeterreadingsys.util.FileNameUtil;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;
import dbhelper.XmlNameHelper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class CopyWaterUploadActivity extends Activity{
 public Button button;//上传按钮
	 
	 public TextView textview1;//需上传总数
	 
	 public TextView textview2;//未上传总数
	 
	 public TextView textview3;//进度条描述
	 
	 public TextView textview4;
	 
	 public ProgressBar pb;//进度条
	 
	 public List<WaterInfoVO> list=null;
	 public boolean b=true;
	 public String imsi;
	 public String url;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.waterbanding_onetoupload);
		init();
	}

	/**
	 * 初始化控件、数据
	 */
	public void init(){
		 SharedPreferenceUtil sh=new SharedPreferenceUtil(CopyWaterUploadActivity.this);
		  imsi=sh.getValue(XmlNameHelper.IMSI);
		  RequestUrl r=new RequestUrl(CopyWaterUploadActivity.this);
		   url=r.getUrl(RequestUrl.COPYWATERLIST);
		
		   button=(Button)findViewById(R.id.waterbanding_upload_button);
		   pb=(ProgressBar)findViewById(R.id.waterbanding_progressBar);
		   textview1=(TextView)findViewById(R.id.waterbanding_upload_total);
		   textview2=(TextView)findViewById(R.id.waterbanding_noupload_total);
		   textview3=(TextView)findViewById(R.id.waterbanding_pb_textview);
		   textview4=(TextView)findViewById(R.id.onekeyupload_info);
		   textview4.setText("抄表数据一键上传");
		   CopyWaterDao dao=new CopyWaterDao(this);
		   final int noupload=dao.queryNoupload();
		   textview1.setText(dao.queryCopyWater()+"");
		   textview2.setText(noupload+"");
		   textview3.setText("0%");
		   dao.close();
		   button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(noupload<=0){
					Toast.makeText(CopyWaterUploadActivity.this, "数据已经上传完了！", Toast.LENGTH_LONG).show();
					return;
				}
				 CopyWaterDao dao=new CopyWaterDao(CopyWaterUploadActivity.this);
				 list=dao.queryAllNoUpload();
				 System.out.println("是否RFID抄表"+list.get(0).getIsNorfid());
				 dao.close();
				
				 if(list==null||list.size()<1){
					 Toast.makeText(CopyWaterUploadActivity.this, "没有上传的数据！", Toast.LENGTH_LONG).show();
					 return;
				 }
				 textview3.setText("正在上传中0%");
				 pb.setMax(list.size());
				if(b){
					myThread.start();
				}else{
					 Toast.makeText(CopyWaterUploadActivity.this, "正在上传中！", Toast.LENGTH_LONG).show();
				}
				
			}
		});
	}
	
	Handler myHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {

			if(msg.what==1){
				b=true;				
				Toast.makeText(CopyWaterUploadActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
				
			}else if(msg.what==2){
				b=false;
				pb.setProgress(msg.arg1);
				CopyWaterDao dao=new CopyWaterDao(CopyWaterUploadActivity.this);
				dao.uploadstatus(list.get(msg.arg1).getId());
				textview1.setText(dao.queryCopyWater()+"");
				textview2.setText(dao.queryNoupload()+"");
				textview3.setText((float)msg.arg1/(float)list.size()*100+"%");
				dao.close();
				if(msg.arg1==list.size()){					
					b=true;
					textview3.setText("100%");
					textview1.setText("0");
					textview2.setText("0");
					Toast.makeText(CopyWaterUploadActivity.this, "上传成功", 1000).show();
					
				}
				
			}
		
			super.handleMessage(msg);
		}

		
		
	};
	
	
	/**
	 * 上传线程
	 */
	Thread myThread=new Thread(){

		@Override
		public void run() {

			
            b=false;
			for(int i=0;i<list.size();i++){
				
				Map<String, String> map3=new HashMap<String, String>();
				Map<String, String> map2=new HashMap<String, String>();
				Map<File, String> map1=new HashMap<File, String>();
				WaterInfoVO waterinfo=list.get(i);
			
				map3.put("waternumber", waterinfo.getWaternumber());
				map3.put("thismonthwater" + "", waterinfo.getThismonthwater()+"");		
				String usewater=(waterinfo.getThismonthwater()-waterinfo.getLastmonthwater())+"";		
				map3.put("usewater", usewater);
				map3.put("thisdate", waterinfo.getThisdate());
				map3.put("imsi",imsi);
				map3.put("desc", waterinfo.getDesc());
				map3.put("sid", "0");
				map3.put("adds", waterinfo.getAdds());
				map3.put("isnorfid", waterinfo.getIsNorfid());
				 System.out.println("是否RFID抄表"+waterinfo.getIsNorfid());
				List<String> lists=waterinfo.getImagepath();
				for(int k=0;k<lists.size();k++){
					File file=new File(lists.get(i));
					if(file.exists()){				
						map2.put(file.getName(), "imagename");
						map1.put(file, "imagepath");
					}							
				}	
				
				String respon=	UploadHttpClient.uploadSubmit(url, map2, map1,map3);
				
				if(respon.equals("error")){
					Message msg=myHandler.obtainMessage();
					msg.what=1;
					myHandler.sendMessage(msg);
				}else{
					try {
						JSONObject json=new JSONObject(respon);
						String message=json.getString("message");
						if(message.equals("yes")){
							Message msg=myHandler.obtainMessage();
							msg.what=2;
							msg.arg1=i;
							msg.obj=waterinfo.getId();
							CopyWaterDao dao=new CopyWaterDao(CopyWaterUploadActivity.this);
							dao.uploadstatus(waterinfo.getId());
							dao.close();
							myHandler.sendMessage(msg);
						}else{
							Message msg=myHandler.obtainMessage();
							msg.what=1;										
							myHandler.sendMessage(msg);
						}
					} catch (JSONException e) {
						Message msg=myHandler.obtainMessage();
						msg.what=1;
						myHandler.sendMessage(msg);
					}
					
				}
			}
		
			
		
			super.run();
		}
		
	};
}
